﻿using System.ComponentModel.Composition;
using Mefisto.Core.Resources.Contracts;
using System.Web.Mvc;
using System.Dynamic;

namespace Mefisto.Core.Mvc.Views
{
    [Export(typeof(MefistoViewPage))]
    public abstract class MefistoViewPage : WebViewPage
    {
        #region Fields

        private ExpandoObject _translationBag;

        #endregion

        #region Properties

        public dynamic TranslationBag
        {
            get { return _translationBag ?? (_translationBag = new ExpandoObject()); }
        }

        /// <summary>
        /// Code de la culture en cours
        /// </summary>
        public string CultureCode
        {
            get
            {
                return Session["CultureCode"].ToString();
            }
            set
            {
                Session["CultureCode"] = value;
            }
        }

        /// <summary>
        /// Code de l'application en cours
        /// </summary>
        public string ApplicationCode
        {
            get
            {
                return Session["ApplicationCode"].ToString();
            }
            set
            {
                Session["ApplicationCode"] = value;
            }
        }

        public IResourceProvider ResourceProvider;

        #endregion

        #region Constructor

        public MefistoViewPage()
        {
            ResourceProvider = DependencyResolver.Current.GetService<IResourceProvider>();
        }

        #endregion

        #region Methods

        public string GetTranslation(string resourceCode)
        {
            ResourceProvider.TranslationBag = TranslationBag;
            return ResourceProvider.GetTranslation(resourceCode, CultureCode, ApplicationCode);
        }

        public void ClearTranslationBag()
        {
            _translationBag = null;
        }

        #endregion
    }

    [Export(typeof(MefistoViewPage))]
    public abstract class MefistoViewPage<T> : WebViewPage<T>
    {
        #region Fields

        private ExpandoObject _translationBag;

        #endregion

        #region Properties

        public dynamic TranslationBag
        {
            get { return _translationBag ?? (_translationBag = new ExpandoObject()); }
        }

        /// <summary>
        /// Code de la culture en cours
        /// </summary>
        public string CultureCode
        {
            get
            {
                return Session["CultureCode"].ToString();
            }
            set
            {
                Session["CultureCode"] = value;
            }
        }

        /// <summary>
        /// Code de l'application en cours
        /// </summary>
        public string ApplicationCode
        {
            get
            {
                return Session["ApplicationCode"].ToString();
            }
            set
            {
                Session["ApplicationCode"] = value;
            }
        }

        public IResourceProvider ResourceProvider;

        #endregion

        #region Constructor

        public MefistoViewPage()
        {
            ResourceProvider = DependencyResolver.Current.GetService<IResourceProvider>();
        }

        #endregion

        #region Methods

        public string GetTranslation(string resourceCode)
        {
            ResourceProvider.TranslationBag = TranslationBag;
            return ResourceProvider.GetTranslation(resourceCode, CultureCode, ApplicationCode);
        }

        public void ClearTranslationBag()
        {
            _translationBag = null;
        }

        #endregion
    }
}